package com.njust.service.Impl;

import com.njust.common.ServerRespone;
import com.njust.dataobject.*;
import com.njust.enums.SelectType;
import com.njust.enums.ZdType;
import com.njust.form.FiveCompareForm;
import com.njust.form.TendencyForm;
import com.njust.mapper.MotorInfoMapper;
import com.njust.mapper.TrainInfoMapper;
import com.njust.mapper.VtInfoMapper;
import com.njust.service.TendencyService;
import com.njust.utils.DateUtil;
import com.njust.utils.StringUtil;
import com.njust.vo.TendencyVO.FiveCompareVO;
import com.njust.vo.TendencyVO.HistoryTendencyVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * @author zjwpro@foxmail.com
 * @date 2018/3/10 下午10:13
 */
@Service
@Slf4j
public class TendencyServiceImpl implements TendencyService {

    @Autowired
    private VtInfoMapper vtInfoMapper;

    @Autowired
    private MotorInfoMapper motorInfoMapper;

    @Autowired
    private TrainInfoMapper trainInfoMapper;

    @Override
    public ServerRespone historyTendency(TendencyForm tendencyForm) throws ParseException{
        List<Long> trainOnlyIdlist = trainInfoMapper.findTrainOnlyIdBytrainIdAndtrainDate(tendencyForm.getTrainId(),
                DateUtil.String2Date(tendencyForm
                        .getStartTime()), DateUtil.String2Date(tendencyForm.getEndTime()));


        if(trainOnlyIdlist.size()==0){
            return ServerRespone.createByErrorMessage("无数据");
        }

        ServerRespone serverRespone = assembleTendencyDataBySelectType(trainOnlyIdlist,
                tendencyForm.getSelectType(), tendencyForm
 .getCarriageNum(), tendencyForm.getWheelNum(),tendencyForm.getMotorNum());
        return serverRespone;
    }

    private ServerRespone assembleTendencyDataBySelectType(List<Long> trainOnlyIdlist, Integer
            selectType, String carriageNum, Integer wheelNum, Integer motorNum) {
        if (selectType.equals(SelectType.ZHENDONG.getCode())) {
            VtInfoExample vtInfoExample = new VtInfoExample();
            VtInfoExample.Criteria criteria = vtInfoExample.createCriteria();
            criteria.andCarriageNumEqualTo(carriageNum);
            criteria.andWheelNumEqualTo(wheelNum);
            criteria.andTrainOnlyidIn(trainOnlyIdlist);

            List<VtInfo> vtInfos = vtInfoMapper.selectByExample(vtInfoExample);

            if (vtInfos.size() == 0) {
                return ServerRespone.createByErrorMessage("无数据");
            }

            Map<String,List<HistoryTendencyVO>> zhendongMap=new HashMap<>();

            List<HistoryTendencyVO> list1=new ArrayList<>();
            List<HistoryTendencyVO> list2=new ArrayList<>();
            List<HistoryTendencyVO> list3=new ArrayList<>();
            List<HistoryTendencyVO> list4=new ArrayList<>();
            List<HistoryTendencyVO> list5=new ArrayList<>();

            for (VtInfo vtInfo : vtInfos) {
                HistoryTendencyVO historyTendencyVO=new HistoryTendencyVO();
                HistoryTendencyVO historyTendencyVO1=new HistoryTendencyVO();
                HistoryTendencyVO historyTendencyVO2=new HistoryTendencyVO();
                HistoryTendencyVO historyTendencyVO3=new HistoryTendencyVO();
                HistoryTendencyVO historyTendencyVO4=new HistoryTendencyVO();

                historyTendencyVO.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                historyTendencyVO1.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                historyTendencyVO2.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                historyTendencyVO3.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                historyTendencyVO4.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));

                historyTendencyVO.setValue(vtInfo.getJfgValue());
                historyTendencyVO1.setValue(vtInfo.getFzValue());
                historyTendencyVO2.setValue(vtInfo.getFzyzValue());
                historyTendencyVO3.setValue(vtInfo.getQdValue());
                historyTendencyVO4.setValue(vtInfo.getQdyzValue());

                list1.add(historyTendencyVO);
                list2.add(historyTendencyVO1);
                list3.add(historyTendencyVO2);
                list4.add(historyTendencyVO3);
                list5.add(historyTendencyVO4);
            }
            zhendongMap.put(ZdType.JFG.getMsg(),list1);
            zhendongMap.put(ZdType.FZ.getMsg(),list2);
            zhendongMap.put(ZdType.FZYZ.getMsg(),list3);
            zhendongMap.put(ZdType.QD.getMsg(),list4);
            zhendongMap.put(ZdType.QDYZ.getMsg(),list5);

            return ServerRespone.createBySuccess(zhendongMap);


        } else if (selectType.equals(SelectType.ZHOUWEN.getCode())) {

            VtInfoExample vtInfoExample = new VtInfoExample();
            VtInfoExample.Criteria criteria = vtInfoExample.createCriteria();
            //车厢号
            criteria.andCarriageNumEqualTo(carriageNum);
            //车轮号
            criteria.andWheelNumEqualTo(wheelNum);
            //列车唯一编号
            criteria.andTrainOnlyidIn(trainOnlyIdlist);

            List<VtInfo> vtInfos = vtInfoMapper.selectByExample(vtInfoExample);

            if (vtInfos.size() == 0) {
                return ServerRespone.createByErrorMessage("无数据");
            }

            List<HistoryTendencyVO> list=new ArrayList<>();
            for (VtInfo vtInfo : vtInfos) {
                HistoryTendencyVO historyTendencyVO=new HistoryTendencyVO();
                historyTendencyVO.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                historyTendencyVO.setValue(vtInfo.getZxTemp());
                list.add(historyTendencyVO);
            }

            return ServerRespone.createBySuccess(list);

        } else {
            MotorInfoExample motorInfoExample = new MotorInfoExample();
            MotorInfoExample.Criteria criteria = motorInfoExample.createCriteria();
            criteria.andCarriageNumEqualTo(carriageNum);

            criteria.andMotorNumEqualTo(motorNum);
            //列车唯一编号
            criteria.andTrainOnlyidIn(trainOnlyIdlist);
            //查询出所有的数据
            List<MotorInfo> motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);

            if (motorInfoList.size() == 0) {
                return ServerRespone.createByErrorMessage("无数据");
            }
            List<HistoryTendencyVO> list=new ArrayList<>();
            for (MotorInfo motorInfo : motorInfoList) {
                HistoryTendencyVO historyTendencyVO=new HistoryTendencyVO();
                historyTendencyVO.setTrainDate(DateUtil.Date2String(motorInfo.getTrainDate()));
                historyTendencyVO.setValue(motorInfo.getMotorTemp());
                list.add(historyTendencyVO);
            }
            return ServerRespone.createBySuccess(list);
        }
    }


    @Override
    public ServerRespone fiveCompare(FiveCompareForm f) throws ParseException {
        //存放 6个时间段的数据 时间为倒序
        List<FiveCompareVO> fiveCompareVOS=new ArrayList<>();

        //查找出最后6条记录  排序为3 2 1
        List<Long> trainOnlyIdList = trainInfoMapper.findLastSixByTrainDateAndTrainId(f.getTrainId(),
                DateUtil.String2Date(f.getStartTime()), DateUtil.String2Date(f.getEndTime()));

        if(trainOnlyIdList.size()==0){
            return ServerRespone.createByErrorMessage("无数据");
        }

        for (Long trainOnlyId:trainOnlyIdList){
            FiveCompareVO fiveCompareVO=new FiveCompareVO();
            if(f.getSelectType().equals(SelectType.ZHENDONG.getCode())){

                VtInfoExample vtInfoExample=new VtInfoExample();

                VtInfoExample.Criteria criteria = vtInfoExample.createCriteria();
                criteria.andTrainOnlyidEqualTo(trainOnlyId);

                List<VtInfo> vtInfos = vtInfoMapper.selectByExample(vtInfoExample);

                if(vtInfos.size()==0){
                    continue;
                }
                Map<String,BigDecimal> jfgmap=new LinkedHashMap<>(64);

                for (VtInfo vtInfo:vtInfos){
                    fiveCompareVO.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                    //jfg fz fzyz qd qdyz
                    if(f.getZdType().equals(ZdType.JFG.getCode())){
                        jfgmap.put(StringUtil.connectStr(vtInfo.getCarriageNum()
                                ,vtInfo.getWheelNum()),vtInfo.getJfgValue());
                    }else if(f.getZdType().equals(ZdType.FZ.getCode())){
                        jfgmap.put(StringUtil.connectStr(vtInfo.getCarriageNum()
                                ,vtInfo.getWheelNum()),vtInfo.getFzValue());
                    }else if(f.getZdType().equals(ZdType.FZYZ.getCode())){
                        jfgmap.put(StringUtil.connectStr(vtInfo.getCarriageNum()
                                ,vtInfo.getWheelNum()),vtInfo.getFzyzValue());
                    }else if(f.getZdType().equals(ZdType.QD.getCode())){
                        jfgmap.put(StringUtil.connectStr(vtInfo.getCarriageNum()
                                ,vtInfo.getWheelNum()),vtInfo.getQdValue());
                    }else {
                        jfgmap.put(StringUtil.connectStr(vtInfo.getCarriageNum()
                                ,vtInfo.getWheelNum()),vtInfo.getQdyzValue());
                    }
                }
                fiveCompareVO.setValue(jfgmap);
                fiveCompareVOS.add(fiveCompareVO);
            }else if(f.getSelectType().equals(SelectType.ZHOUWEN.getCode())){
                VtInfoExample vtInfoExample=new VtInfoExample();

                VtInfoExample.Criteria criteria = vtInfoExample.createCriteria();
                criteria.andTrainOnlyidEqualTo(trainOnlyId);

                List<VtInfo> vtInfos = vtInfoMapper.selectByExample(vtInfoExample);

                if(vtInfos.size()==0){
                    continue;
                }
                Map<String,BigDecimal> jfgmap=new LinkedHashMap<>(64);

                for (VtInfo vtInfo:vtInfos){
                    fiveCompareVO.setTrainDate(DateUtil.Date2String(vtInfo.getTrainDate()));
                    jfgmap.put(StringUtil.connectStr(vtInfo.getCarriageNum()
                            ,vtInfo.getWheelNum()),vtInfo.getZxTemp());
                }
                fiveCompareVO.setValue(jfgmap);
                fiveCompareVOS.add(fiveCompareVO);
            }else if(f.getSelectType().equals(SelectType.MOTOR.getCode())){
                MotorInfoExample motorInfoExample=new MotorInfoExample();
                MotorInfoExample.Criteria criteria = motorInfoExample.createCriteria();

                criteria.andTrainOnlyidEqualTo(trainOnlyId);
                List<MotorInfo> motorInfoList = motorInfoMapper.selectByExample(motorInfoExample);
                if(motorInfoList.size()==0){
                    continue;
                }
                Map<String,BigDecimal> jfgmap=new HashMap<>();
                for (MotorInfo motorInfo:motorInfoList){
                    fiveCompareVO.setTrainDate(DateUtil.Date2String(motorInfo.getTrainDate()));
                    jfgmap.put(StringUtil.connectStr(motorInfo.getCarriageNum()
                            ,motorInfo.getMotorNum()),motorInfo.getMotorTemp());
                }
                fiveCompareVO.setValue(jfgmap);
                fiveCompareVOS.add(fiveCompareVO);
            }else {
                return ServerRespone.createByErrorMessage("无数据");
            }
        }
        return ServerRespone.createBySuccess(fiveCompareVOS);
    }
}
